package com.example.firstjavamqtt.factory.strategy.DeviceLogMessage;

import com.alibaba.fastjson.JSONObject;
import com.example.firstjavamqtt.common.excepiton.GlobalException;
import com.example.firstjavamqtt.common.result.ResultCodeEnum;
import com.example.firstjavamqtt.model.device.Led;
import org.springframework.stereotype.Component;

//生成led操作的设备日志策略
@Component
public class LedLogMessageStrategy implements DeviceLogMessageStrategy {

    @Override
    public String getOperation(String string) {
        Led led = JSONObject.parseObject(string, Led.class);
        System.out.println(led);
        if(!led.isOnline()){
            throw new GlobalException(ResultCodeEnum.OFFLINE);
        }
        //解析led的状态来获得操作
        if (led.getIsOn() == 0 && led.getIsStreamLedOn() == 0) {
            return "关灯";
        }
        if (led.getIsOn() == 1 && led.getIsStreamLedOn() == 0) {
            return "开灯";
        }
        if (led.getIsStreamLedOn() == 1 && led.getIsOn() == 0) {
            return "开始闪烁 速度:" + led.getSpeed() + "ms";
        }

        return "";
    }
}
